/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ikaz;
import tuvalLib.*;
import arayuz.CepMuvakkitT;
import astroLib.Settings;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;
import lang.LocalizationSupport;
import record.WarningPrefer;
import java.util.Calendar;
/**
*
* @author mgeden
*/
public class PresetWarnings {
private byte earlyWarnings[] = {0, 45, 0, 0, 0, 0};
private byte warnings[] = {0, 4, 1, 2, 2, 3};
private double[] fiveTimesAdjusted= new double[7];
public static int closestOne;
public static long timeLeftInMillis;
private TimerTask presetAlarmTask;
private Timer warningAlarmTimer;
final static private long HOUR_IN_MILLISEC =3600000;
private Alert alr;
private static Display mDisplay;
private InputStream in;
private Player player;
private String timesNames[];
private VolumeControl vc;
public PresetWarnings (WarningPrefer warningPref,double[] fiveTimes, double timeInHour,CepMuvakkitT midlet)
{ earlyWarnings=warningPref.getEarlyWarningMinutesofAll();
warnings=warningPref.getWarningofAll();
mDisplay=midlet.getDisplay();
timesNames=new String[]{lang.LocalizationSupport.getMessage("IMSAK"), LocalizationSupport.getMessage("GUNES"), LocalizationSupport.getMessage("OGLE"), LocalizationSupport.getMessage("IKINDI"), LocalizationSupport.getMessage("AKSAM"),LocalizationSupport.getMessage("YATSI")};
for (int i = 0; i <= 5; i++) {
fiveTimesAdjusted[i] = (fiveTimes[i] - earlyWarnings[i] / 60.0);
// System.out.println("fiveTimesAdjusted["+i+"]="+APC_Time.Time(fiveTimesAdjusted[i], true));
}
warningAlarmTimer=new Timer();
timeLeftInMillis=(long)(getTimeLeft(getTimeInHour())*HOUR_IN_MILLISEC);
presetAlarmTask=new TimerTask()
{public void run(){
try {
// System.out.println("BEFOREEEEEEEEEEEEEEEEE");
ExecuteWarning(warnings[closestOne]);
Thread.sleep(10000);
//System.out.println("AFTERRRRRRRRRRRR");
timeLeftInMillis = (long) (getTimeLeft(getTimeInHour())*HOUR_IN_MILLISEC);
// System.out.println("getTimeInHour()=" + getTimeInHour());
timeLeftInMillis = (long) (getTimeLeft(getTimeInHour())*HOUR_IN_MILLISEC);
//System.out.println("timeLeftInMillis=" + timeLeftInMillis);
//System.out.println("closestOne=" + PresetWarnings.closestOne);
if (timeLeftInMillis>0)
{
warningAlarmTimer.schedule(presetAlarmTask, timeLeftInMillis);
SCanvas.isWarningOn=true;
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
};
//System.out.println("timeLeftInMillis="+timeLeftInMillis);
//System.out.println("closestOne="+closestOne);
//System.out.println("getTimeInHour()="+getTimeInHour());
//warningAlarmTimer.cancel();
if (timeLeftInMillis>0)
{
warningAlarmTimer.schedule(presetAlarmTask, timeLeftInMillis);
// System.out.println("SCanvas.isWarningOn="+SCanvas.isWarningOn);
SCanvas.isWarningOn=true;
}
//System.out.println("SCanvas.isWarningOn="+SCanvas.isWarningOn);
// presetAlarmTask.cancel();
}
public double getTimeLeft (double timeInHour)
{
double timeLeft=0;
closestOne=-1;
while ((timeLeft<=0)&&(closestOne<=5)){
closestOne++;
timeLeft=fiveTimesAdjusted[closestOne]-timeInHour;
}
// if (timeLeft<0&&(closestOne==6))
// { closestOne=1;
// timeLeft=timeLeft+24-fiveTimes[5]+fiveTimes[0];
// }
return timeLeft;
}
private void vibrate() {
mDisplay.vibrate(20000);
}
private void ExecuteWarning(byte warning)
{
// System.out.println(timesNames[closestOne] + " okundu " + timesNames[(closestOne + 1) % 6] + " bekliyor");
switch (warning) {
case 0:
// System.out.println("Sessiz");
// AlarmText(intentedOne);
break;
case 1:
AlarmText(closestOne);
break;
case 2:
playTone();
AlarmText(closestOne);
break;
case 3:
playAlarmMidi();
AlarmText(closestOne);
break;
case 4:
playBirdChirp();
AlarmText(closestOne);
break;
case 5:
vibrate();
AlarmText(closestOne);
break;
case 6:
flash();
AlarmText(closestOne);
break;
case 7:
reciteTheAzanAmr();
AlarmText(closestOne);
break;
case 8:
AlarmText(closestOne);
vibrate();
flash();
reciteTheAzanAmr();
break;
case 9:
reciteAzanMp3();
AlarmText(closestOne);
break;
case 10:
// System.out.println("ozel ezan");
reciteSpecialAzan(closestOne);
AlarmText(closestOne);
break;
}
//System.out.println( closestOne );
}
private void playTone() {
try {
Manager.playTone(64, 1000, 100);
} catch (MediaException ex) {
alr = new Alert("Error", "Can't play tone", null, AlertType.ERROR);
alr.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(alr);
}
}
private void AlarmText(int whichOne) {
alr = null;
alr = new Alert(LocalizationSupport.getMessage("WARNING"),timesNames[whichOne]+" "+LocalizationSupport.getMessage("SALATTIME"), null, AlertType.INFO);
alr.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(alr);
}
private void flash() {
mDisplay.flashBacklight(20000);
}
private void playAlarmMidi() {
if (player != null && player.getState() != Player.CLOSED) {
player.close();
}
try {
in = getClass().getResourceAsStream("/res/audio/alarm.mid"); // Read wav file, packaged in the jar file
player = Manager.createPlayer(in, "audio/midi");
player.start();
} catch (Exception e) {
alr = new Alert("Error", "Unable to play midi file!", null, AlertType.ERROR);
alr.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(alr);
}
}
private void playBirdChirp() {
if (player != null && player.getState() != Player.CLOSED) {
player.close();
}
try {
in = getClass().getResourceAsStream("/res/audio/birdchirp.wav"); // Read wav file, packaged in the jar file
player = Manager.createPlayer(in, "audio/x-wav");
player.start();
} catch (Exception e) {
alr = new Alert("Error", "Unable to play midi file!", null, AlertType.ERROR);
alr.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(alr);
}
}
private void reciteTheAzanAmr() {
// Player playerAmr;
if (player != null && player.getState() != Player.CLOSED) {
player.close();
}
try {
in = getClass().getResourceAsStream("/res/audio/ezan.amr"); // Read wav file, packaged in the jar file
player= Manager.createPlayer(in, "audio/amr");
player.realize();
// get volume control for player and set volume to max
vc = (VolumeControl) player.getControl("VolumeControl");
if (vc != null) {
vc.setLevel(Settings.optionPref.getVolumeLevel());
}
// the player can start with the smallest latency
player.prefetch();
player.start();
} catch (Exception e) {
alr= new Alert("Error", "Unable to play sound file!", null, AlertType.ERROR);
alr.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(alr);
}
}
private void reciteAzanMp3() {
if (player != null && player.getState() != Player.CLOSED) {
player.close();
}
try {
in = getClass().getResourceAsStream("/res/audio/ezan.mp3"); // Read wav file, packaged in the jar file
player = Manager.createPlayer(in, "audio/mpeg");
player.realize();
// get volume control for player and set volume to max
vc = (VolumeControl) player.getControl("VolumeControl");
if (vc != null) {
vc.setLevel( Settings.optionPref.getVolumeLevel());
}
// the player can start with the smallest latency
player.prefetch();
player.start();
} catch (Exception e) {
alr = new Alert("Error", "Unable to play sound file!", null, AlertType.ERROR);
alr.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(alr);
}
}
private void reciteSpecialAzan(int intendedOne) {
if (player != null && player.getState() != Player.CLOSED) {
player.close();
}
try {
in = getClass().getResourceAsStream("/res/audio/ezan" + intendedOne + ".mp3"); // Read wav file, packaged in the jar file
player = Manager.createPlayer(in, "audio/mpeg");
player.realize();
// get volume control for player and set volume to max
vc = (VolumeControl) player.getControl("VolumeControl");
if (vc != null) {
vc.setLevel( Settings.optionPref.getVolumeLevel());
}
// the player can start with the smallest latency
player.prefetch();
player.start();
} catch (Exception e) {
alr = new Alert("Error", "Unable to play sound file!", null, AlertType.ERROR);
alr.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(alr);
}
}
public final static int getHour() {
return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
}
public final static int getMin() {
return Calendar.getInstance().get(Calendar.MINUTE);
}
public final static int getSec() {
return Calendar.getInstance().get(Calendar.SECOND);
}
public final static String intTwoDigit(int i) {
return ((i < 10) ? "0" : "") + i;
}
private double getTimeInHour() {
return getHour()+getMin()/60.0+getSec()/3600.0;
}
}